package net.huashitong.Indicators.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.Analysis;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.CompletionClassificationFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.AnalysisService;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.GrossRegionalProductService;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.Indicators.service.ThiIndustryService;
import net.huashitong.Indicators.utils.CodeUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.ThiTitleUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
/**
 * GDP指标展示数据
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/grossRegionalProduct/")
public class GrossRegionalProductAppController extends JsonBaseController {

	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	@Autowired
	private GrossRegionalProductService grossRegionalProductService;
	@Autowired
	private ThiIndustryService thiIndustryService;
	@Autowired
	private AnalysisService analysisService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 查找GDP二级所有数据
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/queryGDPSecView")
	public Message queryGDPSecView(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		//GDP全市各县区排名情况
		IndicatorBreakdownDatabean databean = grossRegionalProductService.queryCountyRank(filter);
		if(ObjectUtils.isNotBlank(databean)){
			map.put("type", "1");
			map.put("dataInner", databean); 
			map.put("unit", "亿元");
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_qua);
			map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GXQZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GXQJDZ);
			map.put("title", "该指标全市排名一览");
			map.put("secTitle", "地区生产总值(GDP)全市各县区排名情况");
			maps.add(map);
		}
		
		// GDP当季同比增长情况
		List<IndicatorBreakdownDatabean> databeans = grossRegionalProductService.queryDevelopmentSituation(filter);
		if(ObjectUtils.isNotBlank(databeans)){
			map = new HashMap<String, Object>();
			map.put("type", "2");
			map.put("data", databeans);
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_qua);
			map.put("title", "该指标发展情况");
			map.put("descOne", ThiTitleUtils.DJTBZZL);
			map.put("descTwo", ThiTitleUtils.DJLJZZL);
			map.put("secTitle", "地区生产总值(GDP)当季同比增长情况");
			maps.add(map);
		}
		
		//GDP各季度绝对值及同比增长情况
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		List<Map<String, Object>> list = grossRegionalProductService.queryAbsoluteValue(filter);
		/*if(ObjectUtils.isNotBlank(list)){
			map = new HashMap<String, Object>();
			map.put("type", "3");
			map.put("data", list);
			map.put("unit", "亿元");
			map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GJTBZZL);
			map.put("descTwo", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GJJDZ);
			map.put("descThi", date.substring(0, 4)+ThiTitleUtils.GJJDZ);
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_qua);
			map.put("title", "该指标各季度同比增长情况");
			map.put("secTitle", "地区生产总值(GDP)各季度"+ThiTitleUtils.JDZ+"及同比增长情况");
			maps.add(map);
		}*/
		
		//GDP累计同比增长情况
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		list = grossRegionalProductService.queryCumulativeGrowth(filter);
		if(ObjectUtils.isNotBlank(list)){
			map = new HashMap<String, Object>();
			map.put("type", "4");
			map.put("data", list);
			map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GJTBZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GJTBZZL);
			map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GJLJJDZ);
			map.put("descFou", date.substring(0, 4)+ThiTitleUtils.GJLJJDZ);
			map.put("unit", "亿元");
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_qua);
			map.put("title", "该指标累计同比增长情况");
			map.put("secTitle", "地区生产总值(GDP)累计"+ThiTitleUtils.JDZ+"及同比增长情况");
			maps.add(map);
		}
		
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		//第一产业完成情况
		List<Map<String, Object>> classification2s = grossRegionalProductService.queryByXZValue(filter);
		if(ObjectUtils.isNotBlank(classification2s)){
			map = new HashMap<String, Object>();
			map.put("type", "8");
			map.put("data", classification2s);
			map.put("unit", "亿元");
			map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.ZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.ZZL);
			map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.LJZ);
			map.put("descFiv", date.substring(0, 4)+ThiTitleUtils.LJZ);
			map.put("descFou", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.WWCBF);
			map.put("descSix", date.substring(0, 4)+ThiTitleUtils.WWCBF);
			map.put("category", CodeUtils.category_HY);
			map.put("categoryDetails", CodeUtils.categoryDetails_NO1);
			map.put("title", "该指标第一产业完成情况");
			map.put("secTitle", "地区生产总值(GDP)第一产业完成情况");
			maps.add(map);
		}
		
		//第二产业完成情况
		classification2s = grossRegionalProductService.queryByBMValue(filter);
		if(ObjectUtils.isNotBlank(classification2s)){
			map = new HashMap<String, Object>();
			map.put("type", "8");
			map.put("data", classification2s);
			map.put("unit", "亿元");
			map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.ZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.ZZL);
			map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.LJZ);
			map.put("descFiv", date.substring(0, 4)+ThiTitleUtils.LJZ);
			map.put("descFou", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.WWCBF);
			map.put("descSix", date.substring(0, 4)+ThiTitleUtils.WWCBF);
			map.put("category", CodeUtils.category_HY);
			map.put("categoryDetails", CodeUtils.categoryDetails_NO2);
			map.put("title", "该指标第二产业完成情况");
			map.put("secTitle", "地区生产总值(GDP)第二产业完成情况");
			maps.add(map);
		}
		
		//第三产业完成情况
		classification2s = grossRegionalProductService.queryByHYValue(filter);
		if(ObjectUtils.isNotBlank(classification2s)){
			map = new HashMap<String, Object>();
			map.put("type", "8");
			map.put("data", classification2s);
			map.put("unit", "亿元");
			map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.ZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.ZZL);
			map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.LJZ);
			map.put("descFiv", date.substring(0, 4)+ThiTitleUtils.LJZ);
			map.put("descFou", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.WWCBF);
			map.put("descSix", date.substring(0, 4)+ThiTitleUtils.WWCBF);
			map.put("category", CodeUtils.category_HY);
			map.put("categoryDetails", CodeUtils.categoryDetails_NO3);
			map.put("title", "该指标第三产业完成情况");
			map.put("secTitle", "地区生产总值(GDP)第三产业完成情况");
			maps.add(map);
		}
		

		map = new HashMap<String, Object>();
		filter.setCurrentMonth(cuDate);
		Analysis analysis = analysisService.getByIndIdAndDate(filter);
		if(ObjectUtils.isNotBlank(analysis)){
		    	map.put("secTitle", analysis.getText());
		 		map.put("type", "0");
		 		map.put("title", "该指标分析");
		 		maps.add(map);
	    }
		
		//第三产业支撑指标完成情况
		filter.setDate(date);
		filter.setHy("行业");
		List<CompletionClassificationDatabean> classificationDatabeans = thiIndustryService.queryByZCZB(filter);
		if(ObjectUtils.isNotBlank(classificationDatabeans)){
			map = new HashMap<String, Object>();
			map.put("type", "10");
			map.put("unit", "");
			map.put("data", classificationDatabeans);
			map.put("title", "该指标各行业分类占比情况");
			map.put("secTitle", "第三产业支撑指标完成情况");
			maps.add(map);
		}
		
		map = new HashMap<String, Object>();
		filter.setCurrentMonth(cuDate);
		filter.setIndustryId("A01");
	    TertiaryIndustry industry = industryService.queryByfilter(filter);
	    if(ObjectUtils.isNotBlank(industry)){
	    	map.put("leadership", industry.getLeadership());
	 		map.put("unitsNumber", industry.getUnitsNumber());
	    }
		map.put("lists", maps);
		map.put("monthOrQuarter", CodeUtils.monthOrQuarter_qua);
		map.put("title", "地区生产总值(GDP)运行情况");
		return coverMessage("200", "获取成功",map);
	}
	/**
	 * GDP全市各县区排名情况
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryCountyRank")
	public Message queryCountyRank(@RequestBody IndicatorBreakdownFilter filter){
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryCountyRank(filter);
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			databean.setRatio(indicatorBreakdown.getRatio());
			databean.setCumulative(Integer.toString(Math.abs(Integer.parseInt(indicatorBreakdown.getCumulative()))));
			databeans.add(databean);
		}
		return coverMessage("200", "获取成功", databeans);
	}
	/**
	 * GDP当季同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping("/queryDevelopmentSituation")
	public Message queryDevelopmentSituation(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		//查找当季和前一个季度数据
		List<IndicatorBreakdown> lists = breakdownService.queryDevelopmentSituation(filter);
		TertiaryIndustry industries = industryService.queryByfilter(filter);
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		String lastDateVal = "";
		String DateVal = "";
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
				lastDateVal = indicatorBreakdown.getComparableCumulation();
			}else {
				DateVal = indicatorBreakdown.getComparableCumulation();
				databean.setRatio(indicatorBreakdown.getRatio());
			}
			
		}
		Double double1 = Double.parseDouble(lastDateVal);
		Double double2 = Double.parseDouble(DateVal);
		Double double3 = double2-double1;
		//查找去年当季和去年当季前一个季度数据
		filter.setCurrentMonth(DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(),-12));
		List<IndicatorBreakdown> indicatorBreakdowns = breakdownService.queryDevelopmentSituation(filter);
		for (IndicatorBreakdown indicatorBreakdown : indicatorBreakdowns) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
				lastDateVal = indicatorBreakdown.getComparableCumulation();
			}else {
				DateVal = indicatorBreakdown.getComparableCumulation();
			}
		}
		double1 = Double.parseDouble(lastDateVal);
		double2 = Double.parseDouble(DateVal);
		Double double4 = double2-double1;
		double3 =(double3-double4)/double4*100;
		databean.setCountyIncrease(industries.getCountyIncrease());
		databean.setCityIncrease(industries.getCityIncrease());
		databean.setConcurrentGrowth(Double.toString(double3));
		databean.setuBIncrease(industries.getuBIncrease());
		return coverMessage("200", "获取成功", databean);
	}
	/**
	 * GDP各季度绝对值及同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/queryAbsoluteValue")
	public Message queryAbsoluteValue(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = detailData(breakdownSs,breakdownDatabeans);
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			for (IndicatorBreakdownDatabean databean : breakdownDatabeans) {
				if(databean.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(breakdownDatabean.getCurrentMonth(),-12))){
					Double double1 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double2 = Double.parseDouble(databean.getSeasonsComparison());
					Double double3 = (double1-double2)/double2*100;
					breakdownDatabean.setConcurrentGrowth(Double.toString(double3));
				}
			}
		}
		return coverMessage("200", "", breakdownDatabeans);
	}
	/**
	 * 数据转换
	 * @param databean
	 * @param breakdown
	 * @return
	 */
	public IndicatorBreakdownDatabean dataConversion(IndicatorBreakdownDatabean databean,IndicatorBreakdown breakdown){
		if(StringUtils.isNotBlank(breakdown.getSeasonsComparison())){
			databean.setSeasonsComparison(breakdown.getSeasonsComparison());
		}
		databean.setDate(breakdown.getDate());
		databean.setCurrentMonth(breakdown.getCurrentMonth());
		return databean;
	}
	/**
	 * 数据处理当季可比价格
	 * @param breakdowns
	 * @param breakdownDatabeans
	 * @return
	 */
	public List<IndicatorBreakdownDatabean> detailData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
		for(int i = 0 ;i<breakdowns.size();i++){
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			IndicatorBreakdown breakdown = breakdowns.get(i);
			if(i == breakdowns.size()-1){
				breakdown.setSeasonsComparison(breakdown.getComparableCumulation());
			}else {
				IndicatorBreakdown breakdown2 = breakdowns.get(i+1);
				Double double1 = Double.parseDouble(breakdown.getComparableCumulation());
				Double double2 = Double.parseDouble(breakdown2.getComparableCumulation());
				breakdown.setSeasonsComparison(Double.toString(double1-double2));
			}
			databean = dataConversion(databean, breakdown);
			breakdownDatabeans.add(databean);
		}
		return breakdownDatabeans;
	}
	/**
	 * GDP累计同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/queryCumulativeGrowth")
	@ResponseBody
	public Message queryCumulativeGrowth(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = detailcumulativeData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = detailcumulativeData(breakdownSs,breakdownDatabeans);
		return coverMessage("200", "", breakdownDatabeans);
	}
	/**
	 * 处理可比累计价格
	 * @param breakdowns
	 * @param breakdownDatabeans
	 * @return
	 */
	public List<IndicatorBreakdownDatabean> detailcumulativeData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
		for(int i = 0 ;i<breakdowns.size();i++){
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			IndicatorBreakdown breakdown = breakdowns.get(i);
			databean.setComparableCumulation(Double.toString(Math.abs(Double.parseDouble(breakdown.getComparableCumulation()))));
			databean.setRatio(breakdown.getRatio());
			databean = dataConversion(databean, breakdown);
			breakdownDatabeans.add(databean);
		}
		return breakdownDatabeans;
	}
	/**
	 * 产业完成情况
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryIndustrialCompletion")
	public Message queryIndustrialCompletion(@RequestBody CompletionClassificationFilter filter){
		List<CompletionClassification> lists =  classificationService.queryByfilter(filter);
		List<CompletionClassificationDatabean> databeans = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassification completionClassification : lists) {
			CompletionClassificationDatabean databean = new CompletionClassificationDatabean();
			databean.setActualCompletionValue(completionClassification.getActualCompletionValue());
			Double double1 = Double.parseDouble(completionClassification.getPlanneValue());
			Double double2 = Double.parseDouble(completionClassification.getActualCompletionValue());
			databean.setNoComplete(Double.toString(double1-double2));
			databean.setSpecificIncrease(completionClassification.getSpecificIncrease());
			databean.setYear(completionClassification.getYear());
			databean.setCompleteAnnual(completionClassification.getCompleteAnnual());
			databean.setCompletionPeriod(completionClassification.getCompletionPeriod());
			databeans.add(databean);
		}
		return coverMessage("200", "数据获取成功", databeans);
	}
}
